package com.exam.demo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.exam.demo.common.Result;
import com.exam.demo.entity.Menu;
import com.exam.demo.mapper.MenuMapper;
import com.exam.demo.service.MenuService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述
 *
 * @author yxj30
 * @date 2022/03/16  19:29
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {


    @Override
    public List<Menu> findMenus(String name) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByAsc("sort_num");
        queryWrapper.like("name", name);
        List<Menu> list = list(queryWrapper);
        //查找一级菜单
        List<Menu> parentNodes = list.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList());
        //找出一级菜单的子菜单,筛选所有数据中pid=父级id的数据就是二级菜单
        for (Menu menu : parentNodes) {
            List<Menu> collect = list.stream().filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList());
            menu.setChildren(collect);
        }
        System.out.println("parentNodes = " + parentNodes);
        return parentNodes;
    }


}
